// pages/permission/selectFloor/selectFloor.js
let http = require('../../../utils/fetch.js')
var api = require('../../../utils/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    plot: '',
    city: '',
    CID:'',
    BID:'',
    UID:'',
    fid:'',
    floorList: [],
    type:'',//点击的类型
    unitList:'',//单元
    levelList:'',//楼层
    unitNum:'',//选择的单元数
    nameLevel:'',//选择的楼层数
    floor:'',//楼栋
    floorNumList:'',//楼牌号列表
    nameFloNum:'',//楼牌号
    HID:'',//楼牌号ID
  },
  toPermission: function(e) {
    console.log(e,'eeee')
    this.setData({
      unitNum:e.currentTarget.dataset.nameunit,
      uid:e.currentTarget.dataset.uid,
      floor:e.currentTarget.dataset.name,
      bid:e.currentTarget.dataset.bid,
    })
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 3]; //上一个页面
    prevPage.setData({
      mydata: {
        cid:this.data.CID,
        city:this.data.city,
        plot:this.data.plot,
        floor:e.currentTarget.dataset.name,
        bid:e.currentTarget.dataset.bid,
        buttonShow:true
        
      }
    })
    wx.navigateBack({//返回
      delta: 2
    })
    // wx.navigateTo({
    //   url: '../../permission/permission?obj='+JSON.stringify({city:this.data.city,plot:this.data.plot,floor:e.currentTarget.dataset.name,bid:e.currentTarget.dataset.bid}),
    // })
  },
  //单元
  toPermissionUnit:function(e){
   
    this.setData({
      unitNum:e.currentTarget.dataset.nameunit,
      uid:e.currentTarget.dataset.uid
    })
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.setData({
      unitdata: {
        city:this.data.city,
        plot:this.data.plot,
        floor:this.data.floor,
        bid:this.data.bid,
       uid:this.data.uid,
       unitNum:this.data.unitNum
        
      }
    })
    wx.navigateBack({//返回
      delta: 1
    })
    
    // wx.navigateTo({
    //   url: '../../permission/permission?obj='+JSON.stringify({city:this.data.city,plot:this.data.plot,floor:e.currentTarget.dataset.name,bid:e.currentTarget.dataset.bid,
    //   uid:this.data.uid,unitNum:this.data.unitNum}),
    // })
  },
  //楼层跳转toPermissionLevel
  toPermissionLevel:function(e){
    this.setData({
      
      nameLevel:e.currentTarget.dataset.namelevel,
      fid:e.currentTarget.dataset.fid
    })
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //上一个页面
    
    prevPage.setData({
      
      leveldata: {
        // city:this.data.city,
        // plot:this.data.plot,
        // floor:e.currentTarget.dataset.name,
        // bid:e.currentTarget.dataset.bid,
        // uid:e.currentTarget.dataset.uid,
       
        fid:this.data.fid,
        nameLevel:this.data.nameLevel
        
      }
    })
    wx.navigateBack({//返回
      delta: 1
    })
    // wx.navigateTo({
    //   url: '../../permission/permission?obj='+JSON.stringify({city:this.data.city,plot:this.data.plot,floor:e.currentTarget.dataset.name,bid:e.currentTarget.dataset.bid,
    //   uid:e.currentTarget.dataset.uid,unitNum:e.currentTarget.dataset.name,fid:e.currentTarget.dataset.fid,nameLevel:e.currentTarget.dataset.name}),
    // })
  },
  //门牌号
  toPermissionFloorNum(e){
    this.setData({
      nameFloNum:e.currentTarget.dataset.nameflonum,
      HID:e.currentTarget.dataset.hid
    })
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //上一个页面
    
    prevPage.setData({
      
      floorNumData: {
        // city:this.data.city,
        // plot:this.data.plot,
        // floor:e.currentTarget.dataset.name,
        // bid:e.currentTarget.dataset.bid,
        // uid:e.currentTarget.dataset.uid,
       
        hid:this.data.HID,
        nameFloNum:this.data.nameFloNum
        
      }
    })
    wx.navigateBack({//返回
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    
    var that = this;
    
    
    that.setData({
      plot: JSON.parse(options.obj).plot,
      city: JSON.parse(options.obj).city,
      CID:JSON.parse(options.obj).CID,
      type:JSON.parse(options.obj).type||'',
      BID:JSON.parse(options.obj).bid||'',
      UID:JSON.parse(options.obj).uid||'',
      fid:JSON.parse(options.obj).fid||'',
    })
    that.getList(JSON.parse(options.obj).CID)
  },
  getList(CID) {
    
    //请求的参数
    var that = this;
   
    if (that.data.type==3) {
      //楼栋
     var  params={
        type:1,
        where:{
          CID:CID
        }
      }
    }else if(that.data.type==4){
      //单元
    var  params={
        type:2,
        where:{
          BID:that.data.BID
        }
    }
  }else if(that.data.type==5){
    
    var  params={
      type:3,
      where:{
        UID:that.data.UID
      }
  }
  }else if(that.data.type==6){
    
    var  params={
      type:4,
      where:{
        FID:that.data.fid
      }
  }
}

  else{
    var  params={
      type:1,
      where:{
        CID:CID
      }
    }
  }
    

    //此处为使用封装的post请求
    http.post(api.getHouse, params).then((res) => {
        // console.log(res, 'eeee')
        
        if (res.code == 0) {
          
          that.setData({
            floorList:res.data||'',
            unitList:res.data||'',
            levelList:res.data||'',
            floorNumList:res.data||'',
          })
          
          
        }
      })
      .catch((errMsg) => {    
        //错误提示信息
        wx.showToast({
          title: errMsg,
          icon: "none"
        })
      });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  // onShow: function (options) {
    
  //   var pages = getCurrentPages();
  //   var currPage = pages[pages.length - 1]; //当前页面
  //   let json = currPage.data.mydata;
  //   console.log(json,'jjjjjjjjjjjj')

  // },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})